package com.wnnight.wys.web.role;

import com.wnnight.wys.entity.config.PropertyStaff;
import com.wnnight.wys.security.JwtUtils;
import com.wnnight.wys.service.config.PropertyStaffService;
import com.wnnight.wys.vo.LoginVo;
import com.wnnight.wys.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @ClassName: LoginController
 * @Description: //TODO
 * @author: dz
 * @date: 2022-09-01-11:32
 */

@Slf4j
@RestController
@RequestMapping("/api/v1/wy")
public class LoginController {
    @Autowired
    private JwtUtils jwtUtils;
    @Autowired
    private PropertyStaffService propertyStaffService;



    @PostMapping("/login")
    public ResultVo login(@RequestBody LoginVo loginVo, HttpSession session, HttpServletResponse response) {
        System.out.println("进入LoginController");
        //        String captcha =GlobalCaptcha.getCode();
//        if(!captcha.equalsIgnoreCase(loginVo.getCode())){
//            return new ResultVo(402, "fail", "验证码错误");
//        }
        PropertyStaff propertyStaff = propertyStaffService.getByAccountAndPassword(loginVo.getAccount(), loginVo.getPassword());
//
////        String captcha = session.getAttribute("captcha").toString();
//        log.debug("111112222:{}", captcha);
//        if(!captcha.equalsIgnoreCase(loginVo.getCode())){
//            return new ResultVo(402, "fail", "验证码错误");
//        }
//        if (propertyStaffService.getByAccount(loginVo.getAccount()) == null) {
//            return new ResultVo(401, "fail", "账号不存在");
//        } else if (propertyStaff == null) {
//            return new ResultVo(4011, "fail", "密码不正确");
//        } else {
//            log.debug("44444444u:{}", propertyStaff.getPassword());
            String jwt = jwtUtils.creatJWT(propertyStaff.getId(), propertyStaff.getAccount(), propertyStaff.getRole().getId());
            response.setHeader("jwt-Headers", jwt);
            return new ResultVo(jwt);

    }
}
